package view;

import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.table.AbstractTableModel;

import controller.Controller;

@SuppressWarnings("serial")
public abstract class TableModelDefault extends AbstractTableModel implements Observer {
	protected static final String INCOMECOLUMN = "\u2211 Einkommen";
	protected static final String SUMCOLUMN = "\u2211 Ausgaben";
	protected static final String BALANCECOLUMN = "\u2191\u2193 Bilanz";
	
	protected static final String YEARSUMROW = "\u2211 Jahr";
	protected static final String MONTHSUMROW = "\u2211 Monat";
	

	protected Controller controller;
	protected LinkedList<String> columnNames = new LinkedList<String>();

	public TableModelDefault(Controller controller) {
		this.controller = controller;
		controller.getModel().addObserver(this);
		parseColumnNames();

	}

	private void parseColumnNames() {
		columnNames.clear();
		// add first column depending on instance
		columnNames.add((this instanceof TableModelYear) ? "Monat" : "Tag");
		
		// add other columns from model
		columnNames.addAll(controller.getModel().getDataSource().getCategories());
		
//		// add account columns // TODO assess accounting
//		columnNames.addAll(controller.getModel().getDataSource().getAccounts());
		
		// add generic calculations
		columnNames.add(INCOMECOLUMN);
		columnNames.add(SUMCOLUMN);
		if(this instanceof TableModelYear) columnNames.add(BALANCECOLUMN);

	}
	
	@Override
	public int getColumnCount() {
		return columnNames.size();
	}
	
	@Override
	public String getColumnName(int i) {
		return columnNames.get(i);
	}

	protected Integer getCategoryForModel(int columnIndex) {
		String columnString = getColumnName(columnIndex);
		return controller.getModel().getDataSource().getCategories().indexOf(columnString);
	}
	
	@Override
	public void update(Observable o, Object arg) {
		fireTableDataChanged();		
	}
}
